package com.yaowk.device.listener;

import com.xiaoleilu.hutool.util.ObjectUtil;
import com.yaowk.device.model.Goods;
import com.yaowk.event.event.CheckStoreEvent;
import com.yaowk.event.event.SendReplenishMsgEvent;
import net.dreamlu.event.EventKit;
import net.dreamlu.event.core.ApplicationListener;

/**
 * Created by yaowk on 17-7-27.
 */
public class CheckStoreListener implements ApplicationListener<CheckStoreEvent> {
    @Override
    public void onApplicationEvent(CheckStoreEvent checkStoreEvent) {
        Integer goodsId = checkStoreEvent.getSource();
        Goods goods = Goods.dao.findById(goodsId);
        if (ObjectUtil.isNotNull(goods)) {
            goods.setStock(goods.getStock() - 1).update();
            if (goods.getStock() <= goods.getCritical()) {
                EventKit.post(new SendReplenishMsgEvent(goods.getId()));
            }
        }
    }
}
